<template>
	<view class="order-pages">
		<!-- #ifndef H5 -->
		<!-- <uni-nav-bar :fixed="true" :shadow="false" :statusBar="true" title="订单" color="#ffffff"
			:backgroundColor="primaryColor">
		</uni-nav-bar> -->
		<!-- <view :style="{height:`${configInfo.navBarHeight}px`}"></view> -->
		<fixed top="0">
		<!-- #endif -->
			<!-- #ifdef H5 -->
			<fixed>
			<!-- #endif -->
				<tab @change="handerTabChange" :list="tabList" :activeIndex="activeIndex*1" :activeColor="primaryColor"
					:width="100/tabList.length + '%'" height="100rpx"></tab>
				<view class="b-1px-b"></view>
			</fixed>

			<view @tap.stop="goDetail(index,'detail')"
				class="item-child mt-md ml-md mr-md pd-lg fill-base radius-16 rel" v-for="(item,index) in list.data"
				:key="index">
				<view v-if="item.is_add">
					<view class="bell-tag flex-center f-icontext c-base abs" :style="{background:primaryColor}">加钟服务
					</view>
					<view class="space-md"></view>
				</view>
				<view class="flex-between pb-lg b-1px-b">
					<view class="f-paragraph c-title max-450 ellipsis"
						@tap.stop="$util.goUrl({openType:'copy',url:item.order_code})">订单号：{{item.order_code}}
					</view>
					<!-- // pay_type 1待支付，2待服务，3技-师接单，4技-师出发，5技-师到达，6服务中，7服务完成 -->
					<view class="f-caption text-bold"
						:style="{color:item.pay_type==1?primaryColor: [2,3,4,5,6].includes(item.pay_type)?subColor: item.pay_type == 7 ? '#11C95E' : '#333'}" v-if="item.is_diet == 0">
						{{statusType[item.pay_type]}}
					</view>
					<view class="f-caption text-bold"
						:style="{color:item.pay_type==1?primaryColor: [2,3,4,5,6].includes(item.pay_type)?subColor: item.pay_type == 7 ? '#11C95E' : '#333'}" v-if="item.is_diet == 1">
						{{statusTypes[item.pay_type]}}
					</view>
				</view>
				<view class="flex-center mt-lg" v-for="(aitem,aindex) in item.order_goods" :key="aindex">
					<!-- #ifdef H5 -->
					<view class="cover radius-16">
						<view class="h5-image cover radius-16"
							:style="{ backgroundImage : `url('${aitem.goods_cover}')`}">
						</view>
					</view>
					<!-- #endif -->
					<!-- #ifndef H5 -->
					<image mode="aspectFill" class="cover radius-16" :src="aitem.goods_cover" style="height: 112px;width: 112px;"></image>
					<!-- #endif -->
					<view class="flex-1 ml-md">
						<view class="flex-between">
							<view class="f-mini-title c-title text-bold max-400 ellipsis">
								{{aitem.goods_name}}
							</view>
							<view class="f-desc c-desc">x{{aitem.num}}</view>
						</view>
						<view class="flex-center" :style="{marginTop:aindex==item.order_goods.length-1?'6rpx':'20rpx'}"
							v-if="item.is_diet == 1">
							<view class="flex-1 flex-y-center f-caption c-caption">
								用餐房间：{{item.house.title}}
							</view>
						</view>
						<view class="flex-center" :style="{marginTop:aindex==item.order_goods.length-1?'6rpx':'20rpx'}"
							v-if="item.is_diet == 0">
							<view class="flex-1 flex-y-center f-caption c-caption">服务{{$t('action.attendantName')}}
								<view class="ml-md max-280 ellipsis">
									{{item.coach_info ? item.coach_info.coach_name : '-'}}
								</view>
							</view>
							<view class="f-caption c-warning" v-if="aitem.refund_num>0">已退x{{aitem.refund_num}}
							</view>
						</view>
						<view class="flex-y-center f-caption c-caption">预约时间<view class="ml-md">{{item.start_time}}
							</view>
						</view>
						<view class="flex-y-center f-caption c-caption">预约时间<view class="ml-md">{{item.time_long}}分钟
							</view>
						</view>
						
						<view class="flex-y-center f-caption c-caption" v-if="aindex==item.order_goods.length-1">总计：
							<view class="flex-y-baseline c-warning text-bold">¥
								<view class="f-title">{{item.pay_price}}</view>
							</view>
						</view>
					</view>
				</view>
				<view class="flex-between">
					<view></view>
					<view class="flex-warp">
						<!-- 待支付 -->
						<block v-if="item.pay_type == 1">
							<button @tap.stop="toCancel(index)" class="clear-btn order" style="padding: 2px 10px;">取消订单</button>
							<button @tap.stop="open7(item.id,index)" class="clear-btn order"
								:style="{color:'#fff',background:primaryColor,borderColor:primaryColor}">去支付</button>
						</block>
						<!-- 支付超时 -->
						<block v-if="item.pay_type == -1 || item.pay_type == 7">
							<button @tap.stop="toCancel(index)" class="clear-btn order">删除</button>
						</block>
						<!-- 待服务 -->
						<button @tap.stop="goDetail(index,'refund')" class="clear-btn order"
							v-if="item.pay_type == 2">申请退款</button>
						<!-- 服务中 -->
						<!-- is_add 加钟订单 -->
						<!-- <block v-if="item.pay_type == 6 && (item.is_add || (!item.is_add && item.can_add_order))">
							<button
								@tap.stop="$util.goUrl({url:`/user/pages/bell/list?id=${item.id}&coach_id=${item.coach_id}`})"
								class="clear-btn order"
								:style="{color:'#fff',background:primaryColor,borderColor:primaryColor}"
								v-if="item.is_diet == 0">{{item.is_add?'升级项目':'升级/加钟'}}</button>
						</block> -->
						<!-- 已完成 -->
						<block v-if="item.coach_id && item.pay_type == 7 && !item.is_add">
							<button @tap.stop="goDetail(index,'evaluate')" class="clear-btn order"
								v-if="!item.is_comment">去评价</button>
							<button @tap.stop="toAgain(index)" class="clear-btn order"
								:style="{color:'#fff',background:primaryColor,borderColor:primaryColor}"
								v-if="item.can_again">再来一单</button>
						</block>
					</view>
				</view>
			</view>

			<u-popup :show="show7" @close="close7" mode="bottom" :round="10">
				<view style="padding:30rpx;">
					<view class="label_l">选择支付方式</view>
					<view class="mt-md ml-lg mr-lg fill-base radius-16">
						<view @tap.stop="toChangeItem(index,2)" class="flex-between pt-lg pb-lg ml-lg mr-lg"
							:class="[{'b-1px-t':index!=0}]" v-for="(item,index) in payList" :key="index">
							<view class="flex-y-center f-title c-title">
								<i class="iconfont mr-md" :class="item.icon"
									:style="{color:item.id==1?primaryColor:'',fontSize:'50rpx'}"></i>
								{{item.title}}
								<view class="f-paragraph c-caption ml-md" v-if="item.id == 2">
									余额{{userInfo.balance || 0}}元
								</view>
							</view>
							<view class="flex-y-center c-caption" :style="{color:payInd == index ? primaryColor:''}">
								<i class="pay-icon iconfont icon-xuanze mr-sm"
									:class="[{'icon-radio-fill':item.is_disabled || payInd == index}]"></i>
							</view>
						</view>
					</view>
					<view class="btns" @click="toPay">确认支付</view>
				</view>
			</u-popup>

			<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading"
				v-if="loading">
			</load-more>
			<abnor v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>
			<view class="space-footer"></view>

			<common-popup @confirm="confirmCancel" ref="cancel_item" type="CANCEL_ORDER" :info="popupInfo">
			</common-popup>
			<common-popup @confirm="confirmDel" ref="del_item" type="DELETE_ORDER" :info="popupInfo"></common-popup>

			<view :style="{height: `${configInfo.tabbarHeight}px`}"></view>
			<tabbar></tabbar>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions,
		mapMutations
	} from "vuex"
	import tabbar from "@/components/tabbar.vue"
	export default {
		components: {
			tabbar
		},
		data() {
			return {
				options: {},
				statusType: {
					'-1': '已取消',
					1: '待支付',
					2: '待服务',
					3: this.$t('action.attendantName') + '接单',
					// 4: this.$t('action.attendantName') + '出发',
					// 5: this.$t('action.attendantName') + '到达',
					6: '服务中',
					7: '已完成',
					// 8: '待转单'
					9:'退款中'
				},
				statusTypes: {
					'-1': '已取消',
					1: '待支付',
					2: '待接单',
					3: '已接单',
					// 4: this.$t('action.attendantName') + '出发',
					// 5: this.$t('action.attendantName') + '到达',
					6: '用餐中',
					7: '已完成',
					// 8: '待转单'
					9:'退款中'
				},
				loading: true,
				popupInfo: {},
				lockTap: false,
				id: 0,
				show7: false,
				oIndex: 0,
				payList: [{
						id: 1,
						icon: 'iconweixinzhifu1',
						title: '微信支付'
					},
					{
						id: 2,
						icon: 'iconqianbao c-balance',
						title: '账户余额',
						is_disabled: false
					}
				],
				payInd: 0,
				userInfo:{}
			}
		},
		computed: mapState({
			activeIndex: state => state.order.activeIndex,
			tabList: state => state.order.tabList,
			param: state => state.order.param,
			list: state => state.order.list,
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo
		}),
		async onLoad(options) {
			let {
				tab = 0
			} = options
			this.updateOrderItem({
				key: 'activeIndex',
				val: tab
			})
			// 支付后需刷新页面
			await this.initIndex()
			this.getuserInfo()
		},
		onShow() {
			this.initIndex()
		},
		onPullDownRefresh() {
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initRefresh();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.loading = true;
			this.getList(this.param.page + 1);
		},
		methods: {
			...mapActions(['getConfigInfo', 'getOrderList']),
			...mapMutations(['updateOrderItem']),
			async getuserInfo() {
				var res = await this.$api.mine.index()
				this.userInfo = res
			},
			async initIndex(refresh = false) {
				// #ifdef H5
				if (!refresh && this.$jweixin.isWechat()) {
					await this.$jweixin.initJssdk();
					this.$jweixin.wxReady(() => {
						this.$jweixin.hideOptionMenu()
					})
				}
				// #endif
				if (!this.configInfo.id || refresh) {
					await this.getConfigInfo()
				}
				await this.getList(1)
			},
			initRefresh() {
				this.initIndex(true)
			},
			async getList(page = 0) {
				if (page) {
					let param = this.$util.deepCopy(this.param)
					param.page = page
					this.updateOrderItem({
						key: 'param',
						val: param
					})
				}
				let {
					list: oldList,
					param,
					tabList,
					activeIndex
				} = this
				param.pay_type = tabList[activeIndex].id
				try {
					await this.getOrderList(param)
					this.loading = false
					this.$util.hideAll()
				} catch (e) {
					this.loading = false
					this.$util.hideAll()
				}
			},
			handerTabChange(index) {
				this.updateOrderItem({
					key: 'activeIndex',
					val: index
				})
				uni.pageScrollTo({
					scrollTop: 0
				})
				this.$util.showLoading()
				this.getList(1)
			},
			// 取消/删除订单
			async toCancel(index) {
				let {
					id,
					order_code,
					order_goods,
					pay_type,
				} = this.list.data[index]
				let {
					goods_cover: image,
				} = order_goods[0]
				this.popupInfo = {
					id,
					name: `系统单号：${order_code}`,
					image,
					index,
				}
				let refs_key = pay_type == 1 ? 'cancel_item' : 'del_item'
				this.$refs[refs_key].open()
			},
			// 确认：取消订单
			async confirmCancel() {
				let {
					id,
					index,
				} = this.popupInfo
				await this.$api.order.cancelOrder({
					id
				})
				if (this.activeIndex == 0) {
					this.list.data[index].pay_type = -1
				} else {
					this.list.data.splice(index, 1)
				}
				this.$util.showToast({
					title: `取消成功`
				})
				this.$refs.cancel_item.close()
			},
			// 确认：删除订单
			async confirmDel() {
				let {
					id,
					index,
				} = this.popupInfo
				await this.$api.order.delOrder({
					id
				})
				this.list.data.splice(index, 1)
				this.$util.showToast({
					title: `删除成功`
				})
				this.$refs.del_item.close()
			},
			open7(id, index) {
				this.show7 = true;
				this.id = id;
				this.oIndex = index
			},
			close7() {
				this.show7 = false;
			},
			toChangeItem(index) {
				console.log(index)
				this.payInd = index;
			},
			// 去支付
			async toPay() {
				if (this.lockTap) return;
				this.lockTap = true;
				this.$util.showLoading()
				let {
					id
				} = this.list.data[this.oIndex]
				let pay_model = this.payList[this.payInd].id
				console.log(pay_model,'pay_model');
				try {
					let pay_list = ''
					if(pay_model == 1){
						let pay = await this.$api.order.rePayOrder({
							id: id,
							pay_model: pay_model
						})
						pay_list = pay.pay_list
					}
					if(pay_model == 2){
						pay_list = await this.$api.order.rePayOrder({
							id: id,
							pay_model: pay_model
						})
					}
					console.log(pay_list, 'pay_list');
					if (pay_list) {
						if (pay_model == 3) {
							pay_list = {
								orderInfo: pay_list,
								provider: 'alipay'
							}
						}

						// #ifdef H5

						if (pay_model == 3) {
							pay_list = Object.assign({}, pay_list, {
								order_id: id,
								page_url: `/pages/order?tab=2`
							})
							this.updateOrderItem({
								key: 'alipayOrderParams',
								val: pay_list
							})
							this.$util.goUrl({
								url: '/user/pages/alipay-result'
							})
							this.lockTap = false
							setTimeout(() => {
								this.initRefresh()
							}, 3000)
							return
						}
						// #endif
						if(pay_model == 1){
						console.log('1');
							this.$util.pay(pay_list)
							this.$util.showToast({
								title: `支付成功`
							})
							if (this.activeIndex == 0) {
								this.list.data[index].pay_type = 2
							} else {
								this.list.data.splice(index, 1)
							}
						}
						if(pay_model == 2){
							this.$util.showToast({
								title: `支付成功`
							})
						}
						this.handerTabChange(2)
						this.show7 = false;
						this.getuserInfo()
						// this.getList(1)
						this.lockTap = false;
					}
					setTimeout(() => {
						this.$util.hideAll()
					},1000)
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			// 去核销
			async toHx(index) {
				let {
					id,
					qr: image
				} = this.list.data[index]
				this.popupInfo = {
					id,
					image
				}
				this.$refs.hx_item.open()
			},
			// 订单详情/申请退款/去评价
			goDetail(index, page) {
				let {
					id
				} = this.list.data[index]
				let url = `/user/pages/order/${page}?id=${id}`
				this.$util.goUrl({
					url
				})
			},
			// 再来一单
			async toAgain(index) {
				let {
					id: order_id,
					coach_id
				} = this.list.data[index]
				await this.$api.order.onceMoreOrder({
					order_id
				})
				this.$util.goUrl({
					url: `/user/pages/order?id=${coach_id}`
				})
			}
		}
	}
</script>


<style lang="scss">
	.btns {
		background-color: #07c160;
		color: #fff;
		border-radius: 18rpx;
		text-align: center;
		width: 80%;
		margin: 30rpx auto 0;
		padding: 20rpx 0;
		font-size: 28rpx;
	}
	.clear-btn{
		padding: 2px 10px;
	}
</style>